#ifndef __TEST_JSON__
#define __TEST_JSON__

#include <nlohmann/json.hpp>

using json = nlohmann::json;

class Person
{
  public:
    std::string name;
    int age;

    // 必须提供默认构造函数
    Person () = default;
    Person (std::string n, int a) : name (std::move (n)), age (a) {}

    // 1. 实现 to_json 函数
    friend void to_json (json &j, const Person &p)
    {
        j = json{{"name", p.name}, {"age", p.age}};
    }

    // 2. 实现 from_json 函数
    friend void from_json (const json &j, Person &p)
    {
        j.at ("name").get_to (p.name); // 带异常检查
        j.at ("age").get_to (p.age);
    }
};

class Team
{
  public:
    std::string team_name;
    std::vector<Person> members;
    Team () = default;

    friend void to_json (json &j, const Team &t)
    {
        j = json{{"team_name", t.team_name}, {"members", t.members}};
    }

    friend void from_json (const json &j, Team &t)
    {
        j.at ("team_name").get_to (t.team_name);
        j.at ("members").get_to (t.members);
    }
};


#endif